/**
 * 定义污水厂管理模块
 */
(function(window, angular, undefined){
	var module = angular.module('water.apps.voc.vocQyManager.vocInfo.PsWwtpAttrManager', ['water.entity.PsWwtpAttr']);
	
	module.controller('vocManagerPsWwtpAttrManagerController',function($wdp,$rootScope,$scope,$PsWwtpAttrService){
		
		/**
		 * 查询条件
		 */
		$scope.queryObject={};
		/**
		 * 污水厂数据
		 */
		$scope.companyData={};
		/**
		 * 编辑数据
		 */
		$scope.companyDataEdit={};
		/**
		 * 编辑页面折叠效果
		 */
		$scope.onEditSide = function(id){
			 var isedithidden = $("#"+id).next("div.editwsclccontent").is(":hidden");
			 $("div.editwsclccontent").slideUp();
			 if(isedithidden){
				 $("#"+id).next("div.editwsclccontent").slideDown();
			 }
			 else{
				 $("#"+id).next("div.editwsclccontent").slideUp();
			 }
		}
		/**
		 * 编辑按钮点击事件
		 */
		$scope.onEditClick = function(data){
			$scope.companyDataEdit = angular.copy(data);
			$scope.editDialog.open();
		}
		/**
		 * 取消按钮点击事件
		 */
		$scope.onCancelClick=function(){
			$scope.companyDataEdit={};
			$scope.editDialog.close();
		};
		/**
		 * 更新数据事件
		 */
		$scope.onUpdateClick=function(){
			$PsWwtpAttrService.updateModel($scope.companyDataEdit).then(function(d){
				$.messager.alert("保存成功","保存成功",'info');
				$scope._getCompanyList();
				$scope.editDialog.close();
			});
		};
		/**
		 * 打开关注度
		 */
		$scope.onOpenLevelData=function(){
			$waterDataCommon.getPageRows("WWTP_ATTENTION","ztxxList",{"WWTP_EXP_NO:like":$scope.queryObject.WWTP_ID},20,1);
			$itfreer.openWindow('GetStatusList');
		};
		/**
		 * 详情界面收合效果
		 */
		$("#baxx h5.wsclchead").bind("click",function(){
			var ishidden = $(this).next("div.wsclccontent").is(":hidden");
			 $("div.wsclccontent").slideUp();
			 if(ishidden){
				 $(this).next("div.wsclccontent").slideDown();
			 }
			 else{
				 $(this).next("div.wsclccontent").slideUp();
			 }
		 });
		/**
		 * 监控污水处理厂id变化
		 */
		if($wdp.viewValue.wwtpDetailParmWatch){
			$wdp.viewValue.wwtpDetailParmWatch();
		}
		$wdp.viewValue.wwtpDetailParmWatch = $rootScope.$watch(function(){return $wdp.viewValue.wwtpDetailParm;},function(){
			if(!$wdp.viewValue.wwtpDetailParm){
				return;
			}
			$scope.queryObject.WWTP_ID = $wdp.viewValue.wwtpDetailParm.COMPANY_CODE;
			$scope._getCompanyList();
        },true);
		/**
		 * 获取企业数据
		 */
		$scope._getCompanyList=function(){
			$PsWwtpAttrService.getGyfqQyList($scope.queryObject).then(function(data){
				if(data.companyDetail.length < 1){
					$scope.companyData = {};
					return;
				}			
					
				var companyData=data.companyDetail[0];
				var pollType=[];
				for(var i=0;i<data.POLL_TYPE.length;i++) {
					if(data.POLL_TYPE[i].S_DOMAINCODE=="9")
					{
						pollType.push(data.POLL_TYPE[i]);
					}						
				}
				$scope.companyPollType = pollType;
				$scope.companyPollTypeChecked = data.POLL_TYPE_CHECKED;
				var pollType="";
				for(var i = 0; i < data.POLL_TYPE_CHECKED.length;i++) {
					pollType+=data.POLL_TYPE_CHECKED[i].S_DOMAINCAPTION+"、";
				}
				pollType=pollType.substring(0,pollType.length-1); 
				var url=companyData.IMG;
				var arr = new Array();
				if(url!=""&&url!=null){
					if(url.indexOf("|") > 0)
					{
						arr = url.split("|");
						companyData.IMGURL=String(arr[arr.length-1].toString().split(":")[0]);
					}
					else{
						arr = url.split(":");
						companyData.IMGURL=String(arr[0]);
					}
				} else{
					companyData.IMGURL=getJsBasePath()+"wdp/images/noPic.png"; 
				}
				$scope.companyData = companyData;	
				//组装RunDate显示到companyInfo.html.
				if (companyData.RUNDATE!=null) {
					var date = new Date(parseInt(companyData.RUNDATE.time,10));
					//月份得+1，且只有个位数时在前面+0
					var month= date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
					//日期为个位数时在前面+0
					var currentDate= date.getDate() < 10 ? "0" +date.getDate() : date.getDate();		    
					//getFullYear得到4位数的年份 ，返回一串字符串
					var result = date.getFullYear()+"-" +month +"-" +currentDate;
					$scope.companyData.RUNDATE=result;
				}
				$scope.companyData.poll_type=pollType;
			});
		};
		
		/*新增企业窗口*/
		$scope.openAddCompanyWin=function(){
			$scope.newCompanyData={};
			$itfreer.openWindow('add_Company');
			//类型默认为未选中
			for(var i=0;i<$scope.companyPollType.length;i++) {
				$scope.companyPollType[i].CHECKED = '0';
			}
		};	
		/*保存企业信息*/
		$scope.saveCompany=function(addData,pollType){
			addData.pollTypes = "9";			
			addData.tableName = "PS_WWTP_ATTR";
			addData.EXP_NO = addData.COMPANY_CODE;
			$waterDataCommon.post("/CompanyInfo/createCompany",addData,function(d){
				$itfreer.closeWindow('add_Company');
				$.messager.alert("提示","保存成功");
				$scope.queryList();
			});
		};
		// 查询污染源企业列表
		$scope.queryList = function() {
			$waterDataCommon.getPageRows("/CompanyInfo/companyList.do",
					"WsclcList", $queryObj.QueryWsclcObj, 8, 1, function(data) {
						updateWWTPParm(data);
					});
		};

		function updateWWTPParm(data) {
			$wdp.viewValue.wsclcList = data;
			if (data.data.length > 0) {
				$scope.selectWWTP(data.data[0]);
			}
		}
		$scope.selectWWTP = function(curRow) {
			$wdp.viewValue.wwtpDetailParm = {
				WWTPID : curRow.GUID,
				WWTP_NAME : curRow.SDENAME,
				MN0 : curRow.MN0,
				MN1 : curRow.MN1,
				SC0 : curRow.SC0,
				SC1 : curRow.SC1,
				STATION_ID:curRow.EXP_NO,
				COMPANY_CODE:curRow.COMPANY_CODE
			};
			$scope.WWTP_ID = curRow.GUID;
			$scope.stationId = curRow.WSCLC_ID;
		};

		/*关闭新增窗口*/
		$scope.closeAddCompanyWin=function(){
			$itfreer.closeWindow('add_Company');
		};
		/*编辑企业窗口*/
		$scope.openEditCompanyWin=function(companyData){
			isShowSave = true;
			$scope.companyEditData = angular.copy(companyData);
			$itfreer.openWindow("edit_Company");
		};
		/*更新企业信息*/
		$scope.updateCompany=function(updateData,pollType){
			updateData.tableName = "PS_WWTP_ATTR";
			var pwk_x = updateData.PWK_X;
			var pwk_y = updateData.PWK_Y;
			updateData.pollTypes = "9";
			updateData.EXP_NO = updateData.COMPANY_CODE;
			$waterDataCommon.post("/CompanyInfo/updateCompany",updateData,function(d){
				$itfreer.closeWindow("edit_Company");
				$.messager.alert("提示","保存成功!");
				$scope.getCompanyDetailByGUID(updateData.COMPANY_CODE,updateData.GUID);
			});
		};
		/*关闭编辑窗口*/
		$scope.closeEditCompanyWin=function(){
			$itfreer.closeWindow("edit_Company");  
		};
		
	});
}(window, angular));
